<template>
  <div class="container">
    <div class="question-container">
      <el-card class="box-card">
        <!-- 消失头 -->
        <el-breadcrumb v-if="this.$route.params.id && this.$route.params.name" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ this.$route.params.id ? this.$route.params.name : '' }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 消失头 -->

        <!--  下划线 -->
        <el-divider v-if="this.$route.params.id && this.$route.params.name" />
        <!--  下划线 -->

        <!-- 头部类容 -->
        <el-row>
          <el-col :span="18">
            <el-form label-width="80px">
              <el-form-item label="标签名称" style="display: inline-block">
                <el-input v-model="obj.tagName" style="width: 200px; height: 33px" />
              </el-form-item>
              <el-form-item label="状态" style="display: inline-block">
                <el-select v-model="obj.state" placeholder="请选择" clearable>
                  <el-option label="启用" :value="1" />
                  <el-option label="禁用" :value="0" />
                </el-select>
                <span style="margin-left: 10px">
                  <el-button size="mini" @click="clear">清除</el-button>
                  <el-button type="primary" size="mini" @click="search">搜索</el-button>
                </span>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-button v-if="detail.id && detail.name" type="text" class="el-icon-back" @click="$router.back()">返回学科</el-button>
            <el-button class="el-icon-edit" type="success" size="small" style="text-align: right" @click="add">新增标签</el-button>
          </el-col>
        </el-row>
        <el-alert type="info" show-icon :closable="false">
          <template title>
            <span>数据一共{{ this.counts }}条</span>
          </template>
        </el-alert>
        <!-- 头部类容 -->
        <!-- table列表 -->
        <el-table :data="tableData" style="width: 100%; margin: 15px">
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column prop="subjectName" label="所属学科" width="210" />
          <el-table-column prop="tagName" label="标签名称" />
          <el-table-column prop="username" label="创建者" />
          <el-table-column label="创建日期">
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="{ row }">
              {{ !!row.state ? '已启用' : '已禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240">
            <template slot-scope="{ row }">
              <el-button type="text" @click="forbidden(row)">
                <template>
                  {{ !row.state ? '启用' : '禁用' }}
                </template>
              </el-button>
              <el-button v-if="row.state === 1" type="text" disabled @click="edit(row)">修改</el-button>
              <el-button v-else type="text" @click="edit(row)">修改</el-button>
              <el-button v-if="row.state === 1" type="text" disabled @click="Delete(row)">删除</el-button>
              <el-button v-else type="text" @click="Delete(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- table列表 -->
        <!-- 分页 -->
        <div class="block" style="text-align: right">
          <el-pagination
            :current-page="obj.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="obj.pagesize"
            :total="this.counts"
            background
            layout="prev, pager, next,sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
    <!-- 新增标签 弹层 -->

    <TagsAdd ref="tags" :showlist.sync="showlist" :showadd="showadd" :select="selectTable" @loadtagsList="loadtagsList" />
    <!-- 新增标签 弹层 -->
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'

import TagsAdd from '../components/tags-add.vue'
export default {
  components: { TagsAdd },
  data() {
    return {
      form: {},
      counts: 0,
      obj: {
        page: 1, // 页数
        pagesize: 10, // 一页有多少条
        tagName: '',
        state: null
      },

      tableData: [],
      selectTable: [], // 所属学科列表
      openDialog: false,

      showlist: false, // 控制弹层是否显示
      showadd: true // 控制弹层是修改或添加 true 添加 false 修改
    }
  },
  computed: {
    detail() {
      return this.$route.params || null // 获取 学科组件用this.$route.params传过来的 对象
    }
  },
  created() {
    this.loadtagsList()
    this.loadSimpel()
  },

  methods: {
    handleSizeChange(val) {
      this.obj.pagesize = val
      this.loadtagsList()
    },
    handleCurrentChange(val) {
      this.obj.page = val
      this.loadtagsList()
    },
    // 渲染列表
    async loadtagsList() {
      this.obj.subjectID = this.detail.id || null // 向obj 对象 中添加 subjectID 属性  学科组件传过来的 id 值
      const res = await list(this.obj) // 搜索 传过来的id
      this.counts = res.data.counts // 总数居
      this.tableData = res.data.items
    },
    // 渲染简单列表
    async loadSimpel() {
      const res1 = await simple()

      this.selectTable = res1.data // 获取所属学科列表
    },
    // 点击搜索
    async search() {
      const res = await list(this.obj)
      this.tableData = res.data.items
    },
    // 点击清除
    clear() {
      this.obj.tagName = ''
      this.obj.state = null
      this.loadtagsList()
    },
    // 禁用或启用按钮
    async forbidden(row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        row.state = row.state === 1 ? 0 : 1
      } catch (err) {
        console.log(err)
      }
      this.$message.success('操作成功')
    },
    // 点击添加
    add() {
      this.showlist = true // 打开弹层
      this.showadd = true // 将弹层的文字修改为 添加
    },
    // 点击修改
    edit(row) {
      this.showlist = true // 打开新增标签弹层
      this.showadd = false // 将弹层的文字修改为 修改
      this.$refs.tags.getName(row)
    },
    // 点击删除
    Delete(row) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.loadtagsList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="less">
.question-container {
  padding: 0 10px;
  margin: 10px 0;
  /deep/.el-alert__icon {
    font-size: 16px;
    width: 16px;
  }
}
</style>
